package fr.univnantes.lina.ftbcleaner;

import java.util.HashMap;
import java.util.Map;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import fr.univnantes.lina.ftbcleaner.WordLemma;
import fr.univnantes.lina.mlnlp.connectors.ftb.FTBXMLSchema;

public class FTBPropertiesMultiWordCollector {

	public FTBPropertiesMultiWordCollector() {
		super();
	}

	/**
	 * Compile a list of mw ngu  
	 * @param indent
	 * @param n
	 */
	public Map<String,WordLemma> collectPropertiesFromMultiWordNodeList(NodeList nodeList) {
	
		int distinctLemma = 0;
		int numberOfOccurrences = 0;
	
		Map<String,WordLemma> multiWordsExpressionHashMap = new HashMap<String,WordLemma>() ;
	
		// for each mw occurrences
		for(int i=0; (i < nodeList.getLength()); i++) {
			if (nodeList.item(i) instanceof Element) {
				Element aMWEe = (Element) nodeList.item(i);
	
				// Compute MW characteristics
				String lemma = aMWEe.getAttribute(FTBXMLSchema.LEMMA_ATTRIBUTE);
				String cat = aMWEe.getAttribute(FTBXMLSchema.CAT_ATTRIBUTE);
				String subcat = aMWEe.getAttribute(FTBXMLSchema.SUBCAT_ATTRIBUTE);
				String mph = aMWEe.getAttribute(FTBXMLSchema.MPH_ATTRIBUTE);
				String catints = ""; 
				String words = "";
				//Boolean hasPartOfAmalgame = false;
				Boolean hasEmptyCatints = false;
	
				NodeList partOfAMultiWordNodeList = aMWEe.getChildNodes();
	
				//NodeList partOfMultiWordsnodeList  = null;
				//try {
				//	partOfMultiWordsnodeList = (NodeList) partOFMWxpath.evaluate(partOFMWexpression,aMWEe, XPathConstants.NODESET);
				//} catch (XPathExpressionException ex) {
				//	// TODO Auto-generated catch block
				//	ex.printStackTrace();
				//}
	
				// for each word partof mw 
				//int numberOfPartOfWordsNotPartOfAmalgame = 0;
				int numberOfPartOfWords = 0; // should be partOfMultiWordsnodeList.getLength()
				for(int j=0; (j < partOfAMultiWordNodeList.getLength()); j++) {
	
					//TODO assumed the elements are w
					if (partOfAMultiWordNodeList.item(j) instanceof Element) {
						Element aPOMWE = (Element) partOfAMultiWordNodeList.item(j);
	
						String currentCatint = aPOMWE.getAttribute(FTBXMLSchema.CATINT_ATTRIBUTE);
						if (currentCatint.trim().equalsIgnoreCase("")) hasEmptyCatints = true;
						catints += " " + currentCatint;
						words += " " + aPOMWE.getTextContent().trim();
						//
						//NodeList fils = aPOMWE.getChildNodes();
						// Si empty element
						//if (fils.getLength() == 0) {
						//	hasPartOfAmalgame = true;
						//}
						//else 
						numberOfPartOfWords++;
					}
					//else {
					//System.err.println("Error: the current node is not an element as espected !");
					// not an error some of the mw have also text as nodes
					//}
				}
				catints = catints.trim();
				words = words.trim();
	
				// if the lemma of the current mw node is known then update
				if (multiWordsExpressionHashMap.containsKey(lemma)) {
					//System.out.println("Info: Update");
					numberOfOccurrences++;
					multiWordsExpressionHashMap.put(lemma, (WordLemma) multiWordsExpressionHashMap.get(lemma).
							updateMultiWordExpression(cat, subcat, mph, catints, words,  hasEmptyCatints,
									numberOfPartOfWords));
				}
				// else create with initial values
				else {
					numberOfOccurrences++;
					distinctLemma++;
					multiWordsExpressionHashMap.put(lemma,new 
							WordLemma(lemma, cat, subcat, mph, catints, words, hasEmptyCatints, 
									numberOfPartOfWords));
				}
			}
			else {
				System.err.println("Error: the current node is not an element as espected !");
			}
		}
		// in the whole document
		System.err.println("Info: # of multi-words lemma = "+distinctLemma+ ", occurrences = "+numberOfOccurrences);
		//System.err.println("Info: total # of multi-words occurrences = "+numberOfOccurrences);
		//System.err.println("Info: total # of multi-words occurrences in the whole document nodeList.getLength()  = "+nodeList.getLength());
	
		return multiWordsExpressionHashMap;
	}

}